package com.igeek.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.boot.dto.DeptEventStatisticDTO;
import com.igeek.boot.dto.EventStatisticDTO;
import com.igeek.boot.dto.MonthDateCountDTO;
import com.igeek.boot.entity.ZhrjAlarmInfo;
import com.igeek.boot.mapper.ZhrjAlarmInfoMapper;
import com.igeek.boot.service.ZhrjAlarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @package com.igeek.boot.service.impl
 * @Description 事件信息业务实现类
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/12/12 20:06
 */
@Service
public class ZhrjAlarmInfoServiceImpl extends ServiceImpl<ZhrjAlarmInfoMapper, ZhrjAlarmInfo> implements ZhrjAlarmInfoService {
    @Autowired
    private ZhrjAlarmInfoMapper zhrjAlarmInfoMapper;
    public List<EventStatisticDTO> findByType() {
        // 封装查询条件 - 进行分组groupBy进行查询 - 然后统计总数
        QueryWrapper<ZhrjAlarmInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("count(*) as count,event_type")
                .groupBy("event_type");

        // 执行查询并处理结果
        List<Map<String, Object>> resultList = zhrjAlarmInfoMapper.selectMaps(queryWrapper); // 使用selectMaps来获取结果的Map列表

        // 将查询结果转换为EventStatisticDTO列表
        List<EventStatisticDTO> resultDTOs = resultList.stream()
                .map(result -> {
                    // 从Map中提取值并创建DTO
                    Integer eventType = Integer.valueOf(result.get("event_type").toString()) ;
                    Integer count = Integer.valueOf(result.get("count").toString());
                    return new EventStatisticDTO(eventType, count);
                })
                .collect(Collectors.toList());
        return resultDTOs;
    }

    @Override
    public List<DeptEventStatisticDTO> findByDeptId() {
        // 封装查询条件 - 进行分组groupBy进行查询 - 然后统计总数
        QueryWrapper<ZhrjAlarmInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("count(*) as count,dept_id")
                .groupBy("dept_id");
        // 执行查询并处理结果
        List<Map<String, Object>> resultList = zhrjAlarmInfoMapper.selectMaps(queryWrapper);

        // 将查询结果转换为EventStatisticDTO列表
        List<DeptEventStatisticDTO> list = resultList.stream()
                .map(result -> {
                    // 从Map中提取值并创建DTO
                    Integer eventType = Integer.valueOf(result.get("dept_id").toString());
                    Integer count = Integer.valueOf(result.get("count").toString());
                    return new DeptEventStatisticDTO(eventType, count);
                }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<MonthDateCountDTO> findMonthDateCount(LocalDate dateTime, LocalDate nextMonthTime) {
        //封装数据条件
        QueryWrapper<ZhrjAlarmInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("count(*) as count, DATE(event_time) as date")
                .ge("event_time", dateTime)
                .lt("event_time", nextMonthTime)
                .groupBy("date");
        //执行查询并封装结果
        List<Map<String, Object>> resultList = zhrjAlarmInfoMapper.selectMaps(queryWrapper);

        //将结果集转换成DTO
        List<MonthDateCountDTO> dtos = resultList.stream()
                .map(result -> {
                    // 从Map中提取值并创建DTO
                    LocalDate localDate = LocalDate.parse(result.get("date").toString());
                    Integer count = Integer.valueOf(result.get("count").toString());
                    return new MonthDateCountDTO(localDate, count);
                }).collect(Collectors.toList());
        return dtos;
    }

    @Override
    public Page<ZhrjAlarmInfo> pageByZhrj(Integer current, Integer size, ZhrjAlarmInfo query) {
        //创建分页对象
        Page<ZhrjAlarmInfo> page = new Page<>(current,size);
        //封装分页条件
        LambdaQueryWrapper<ZhrjAlarmInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<ZhrjAlarmInfo> pages = zhrjAlarmInfoMapper.selectPage(page, null);
        return pages;
    }
}
